package fr.umlv.m2.uwp2011.web.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.umlv.m2.uwp2011.service.testLoyaute.User;
import fr.umlv.m2.uwp2011.service.testLoyaute.UserService;
import fr.umlv.m2.uwp2011.service.testLoyaute.UserServiceAsync;

public class Application implements EntryPoint {

	private final UserServiceAsync userService = GWT.create(UserService.class);

	@Override
	public void onModuleLoad() {
		/**************************** Create widgets *****************************/
		final Label titlePage = new Label();

		final FlexTable form = new FlexTable();
		final TextBox loginField = new TextBox();
		final PasswordTextBox passwordField = new PasswordTextBox();
		final Button loginButton = new Button("Valider");

		final VerticalPanel loginPage = new VerticalPanel();

		/******************************** Sets widgets ********************************/
		titlePage.setText("University Web Portal");

		form.setText(0, 0, "Authentification");
		form.setText(1, 0, "Identifiant :");
		form.setWidget(1, 1, loginField);
		form.setText(2, 0, "Mot de passe :");
		form.setWidget(2, 1, passwordField);
		form.setWidget(3, 1, loginButton);

		/**************************** Add style on widgets ****************************/
		titlePage.addStyleName("loginTitle");

		form.setWidth("411px");
		form.setStyleName("loginForm");
		form.setCellSpacing(2);

		form.getFlexCellFormatter().setColSpan(0, 0, 2);
		form.getFlexCellFormatter().setHorizontalAlignment(0, 0, HasAlignment.ALIGN_CENTER);
		form.getFlexCellFormatter().setHeight(0, 0, "22px");
		form.getFlexCellFormatter().setStyleName(0, 0, "loginTitleForm");

		form.getFlexCellFormatter().setWidth(1, 0, "160px");
		form.getFlexCellFormatter().setHorizontalAlignment(1, 0, HasAlignment.ALIGN_RIGHT);
		form.getFlexCellFormatter().setStyleName(1, 0, "loginItemFormTopSpacing");
		form.getFlexCellFormatter().setStyleName(1, 1, "loginItemFormTopSpacing");

		form.getFlexCellFormatter().setWidth(1, 0, "160px");
		form.getFlexCellFormatter().setHorizontalAlignment(2, 0, HasAlignment.ALIGN_RIGHT);

		form.getFlexCellFormatter().setStyleName(3, 1, "loginButtonForm");

		loginPage.setWidth("100%");
		loginPage.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);

		/**************************** Add widgets on page *****************************/
		loginPage.add(titlePage);
		loginPage.add(form);

		RootPanel.get("content").add(loginPage);

		/****************************** Click on button *******************************/
		loginButton.addClickHandler(new ClickHandler() {

			AsyncCallback<User> callback = new AsyncCallback<User>() {

				@Override
				public void onFailure(Throwable arg0) {
					System.out.println("failure : " + arg0.getMessage());
				}

				@Override
				public void onSuccess(User user) {
					Window.alert("Authentification success : " + user.getLogin() +" - " + user.getPassword());
					System.out.println("success");
				}
			};
			
			@Override
			public void onClick(ClickEvent event) {
				if(loginField.getText().equals("etudiant") && passwordField.getText().equals("etudiant")) {
					System.out.println(loginField.getText());
					System.out.println(passwordField.getText());
				}
				else {
					System.out.println("wrong login or password");
				}
				userService.findUser(loginField.getText(), passwordField.getText(), callback);
			}
		});
	}
}
